package com.GlobalPaint.app.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.ui.Mine.Personal.PersonalZhanshi;
import com.GlobalPaint.app.ui.location.FriendsLocationActivity;
import com.GlobalPaint.app.ui.location.MyFriendsFragment;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.activities.ChatActivity;
import com.jzy.message.listener.MsgListener;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class MyFriendsAdapter extends XRecyclerView.Adapter<MyFriendsAdapter.ViewHoler>{
    private List<MyFriendBean.DataBean> bean;
    private MyFriendsFragment myFriendsFragment;
    public MyFriendsAdapter(List<MyFriendBean.DataBean> data, MyFriendsFragment myFriendsFragment) {
        bean=data;
        this.myFriendsFragment=myFriendsFragment;
    }

    @Override
    public MyFriendsAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.frament_friends_item, parent, false);
        MyFriendsAdapter.ViewHoler holder = new MyFriendsAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyFriendsAdapter.ViewHoler holder, final int position) {
        String Phone=bean.get(position).getJid()==
                null?"":bean.get(position).getJid();
        if (Phone.length() > 4) {
            String phone1 = (Phone.substring(0, 3));
            String phone2 = Phone.substring(Phone.length() - 3, Phone.length());
            holder.tv_name.setText(phone1 + "***" + phone2);
        }
        String nick=bean.get(position).getNick()==
                null?bean.get(position).getJid():bean.get(position).getNick();
        String Url= bean.get(position).getAvatar()== null?"wu":
                bean.get(position).getAvatar();
        if(Url.equals("wu")){
            Picasso.with(App.context).load(R.drawable.touxiang).into(holder.imge);
        }else {
            Picasso.with(App.context).load(Constants.JASON_SERVICE_URL_TOUXIANG+Url)
                    .placeholder(R.drawable.touxiang).into(holder.imge);
        }
        holder.tv_nick.setText(nick);

        String num= MsgListener.jian().split("@")[0];
        String niche=MsgListener.jian().split("@")[1];
        if(niche.equals(Phone)){
            if(num.equals("1")){
                holder.img_hd.setVisibility(View.VISIBLE);
            }else {
                holder.img_hd.setVisibility(View.GONE);
            }
        }else {
            holder.img_hd.setVisibility(View.GONE);
        }


        holder.tv_guiji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(view.getContext(), FriendsLocationActivity.class);
                intent.putExtra("userid",bean.get(position).getUserId());
                view.getContext().startActivity(intent);
            }
        });
  /*      holder.mview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), ChatActivity.class);
                    intent.putExtra("from", bean.get(position).getJid());
                    view.getContext().startActivity(intent);
            }
        });*/
        holder.rl_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), ChatActivity.class);
                intent.putExtra("from", bean.get(position).getJid());
                intent.putExtra("avatar", bean.get(position).getAvatar());
                intent.putExtra("nicheng", bean.get(position).getNick());
                if(DataManager.userEntity!=null){
                    intent.putExtra("avatarmy", DataManager.userEntity.getAvatar());
                }
                view.getContext().startActivity(intent);
            }
        });
        holder.imge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), PersonalZhanshi.class);
                intent.putExtra("userid",bean.get(position).getUserId());
                view.getContext().startActivity(intent);
            }
        });

        holder.btndel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String Jid=bean.get(position).getJid();
                myFriendsFragment.deleteUser(Jid,position);
                holder.swi.smoothClose();
            }
        });

    }

    @Override
    public int getItemCount() {
        return bean.size();
    }

    public class ViewHoler extends RecyclerView.ViewHolder {

        private final TextView tv_name;
        private final TextView tv_nick;
        private final TextView tv_guiji;
        private final ImageView imge;
        private final SwipeMenuLayout swi;
        private final Button btndel;
        private final RelativeLayout rl_share;
        private final ImageView img_hd;

        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_nick = (TextView)itemView.findViewById(R.id.tv_nick);
            tv_guiji = (TextView)itemView.findViewById(R.id.tv_guiji);
            imge = (ImageView)itemView.findViewById(R.id.imge);
            swi = (SwipeMenuLayout) itemView.findViewById(R.id.swi);
            btndel = (Button)itemView. findViewById(R.id.btndel);
            rl_share = (RelativeLayout)itemView. findViewById(R.id.rl_share);
            img_hd = (ImageView)itemView. findViewById(R.id.img_hd);

        }
    }
}
